<?php
namespace app\website\model;
use app\BaseModel;
use think\facade\Db;

/**
 * @mixin think\Model
 */
class CardModular extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_modular';
    //获取列表
	function listGroup($page,$list_rows,$where = [])
	{


		$result = $this->where($where)->whereNotIn('type','6,9,10')->order('top desc,id desc')->paginate( [ 'list_rows' => $list_rows, 'page' => $page ])->each(function($item, $key){

            $auth_info = Db::name( $item['table_name'])->limit($item['list_limit'])->where('status',1)->where('modular_id',$item['id'])->order('top desc,create_time desc')->select()->toArray();
            foreach ($auth_info as $k=>$v){
                if($item['type']==1){
                    $auth_info[$k]['create_time'] = date('Y/m/d',$v['create_time']);
                }
                if($item['type']==4){
                    $auth_info[$k]['covers'] = [
                        [
                            'width'=> 28,
                            'height'=> 28,
                            'latitude'=> $v['latitude'],
                            'longitude'=> $v['longitude'],
                            'iconPath'=> 'https://lbqnyv2.migugu.com/images/12/2018/11/A33zQycihMM33y337LH23myTqTl3tl.png',
                             'callout'=> [
                                 'content'=>$v['address'],
                                 'fontSize'=>14,
                                 'bgColor'=>'#ffffff',
                                 'padding'=>4,
                                 'display'=>'ALWAYS',
                                 'textAlign'=>'center',
                                 'borderRadius'=>2
                             ]
                        ]
                    ];
                }
                if($item['type'] ==7){
                    if(strstr($v['video'], '/v.qq.com/')){
                        $auth_info[$k]['vid'] = getStr($v['video']);
                    }else{
                        $auth_info[$k]['video'] = transImagesOne($v, ['video'])['video'];
                    }
                }
                if(!empty($v['content']) && strstr($v['content'], '/v.qq.com/')){
                    $auth_info[$k]['content'] =  getimgs($v['content']);
                }
                if(!empty($v['introduction']) && strstr($v['introduction'], '/v.qq.com/')){
                    $auth_info[$k]['introduction'] =  getimgs($v['introduction']);
                }
                if($item['type'] ==2){
                    $auth_info[$k]['content']      = str_replace("&amp;nbsp;","",getimgs($v['content']));
                    $auth_info[$k]['content1']      = str_replace("&amp;nbsp;","",getimgsV2($v['content']));
//                    unset($auth_info[$k]['content']);
                    $auth_info[$k]['introduction'] = str_replace("&amp;nbsp;","",getimgs($v['introduction']));
                    $auth_info[$k]['introduction1'] = str_replace("&amp;nbsp;","",getimgsV2($v['introduction']));
                }
            }
            $item['list_info'] = transImagesOne($auth_info, ['cover']);
            return $item;
        });
		return $result;
	}

    /**
     * @param $page
     * @param $list_rows
     * @param array $where
     * @功能说明:去掉content
     * @author chenniang
     * @DataTime: 2020-04-14 17:07
     */
    function listGroupV2($page,$list_rows,$where = [])
    {


        $result = $this->where($where)->whereNotIn('type','6,9,10')->order('top desc,id desc')->paginate( [ 'list_rows' => $list_rows, 'page' => $page ])->each(function($item, $key){

            $auth_info = Db::name( $item['table_name'])->limit($item['list_limit'])->where('status',1)->where('modular_id',$item['id'])->order('top desc,create_time desc')->select()->toArray();
            foreach ($auth_info as $k=>$v){
                if($item['type']==1){
                    $auth_info[$k]['create_time'] = date('Y/m/d',$v['create_time']);
                }
                if($item['type']==4){
                    $auth_info[$k]['covers'] = [
                        [
                            'width'=> 28,
                            'height'=> 28,
                            'latitude'=> $v['latitude'],
                            'longitude'=> $v['longitude'],
                            'iconPath'=> 'https://lbqnyv2.migugu.com/images/12/2018/11/A33zQycihMM33y337LH23myTqTl3tl.png',
                            'callout'=> [
                                'content'=>$v['address'],
                                'fontSize'=>14,
                                'bgColor'=>'#ffffff',
                                'padding'=>4,
                                'display'=>'ALWAYS',
                                'textAlign'=>'center',
                                'borderRadius'=>2
                            ]
                        ]
                    ];
                }
                if($item['type'] ==7){
                    if(strstr($v['video'], '/v.qq.com/')){
                        $auth_info[$k]['vid'] = getStr($v['video']);
                    }else{
                        $auth_info[$k]['video'] = transImagesOne($v, ['video'])['video'];
                    }
                }
                if(!empty($v['content']) && strstr($v['content'], '/v.qq.com/')&&$item['type'] !=2){
                    $auth_info[$k]['content'] =  getimgs($v['content']);
                }
                if(!empty($v['introduction']) && strstr($v['introduction'], '/v.qq.com/')){
                    $auth_info[$k]['introduction'] =  getimgs($v['introduction']);
                }
                if($item['type'] ==2){
                    unset($auth_info[$k]['content']);
                    $auth_info[$k]['introduction'] = str_replace("&amp;nbsp;","",getimgs($v['introduction']));
                    $auth_info[$k]['introduction1'] = str_replace("&amp;nbsp;","",getimgsV2($v['introduction']));
                }
                if(in_array($item['type'],[1,3])&&!empty($auth_info[$k]['content'])){

                    unset($auth_info[$k]['content']);

                }
            }
            $item['list_info'] = transImagesOne($auth_info, ['cover']);
            return $item;
        });
        return $result;
    }


    /**
     * @param $list_rows
     * @param array $where
     * @功能说明:diy版本
     * @author chenniang
     * @DataTime: 2020-06-15 13:48
     */
    function listGroupV2Diy($list_rows,$where = [])
    {

        $item = $this->where($where)->whereNotIn('type','6,9,10')->find();

        if(!empty($item)){

            $item = $item->toArray();

            $list_rows = !empty($list_rows)?$list_rows:$item['list_limit'];

            $auth_info = Db::name( $item['table_name'])->limit($list_rows)->where('status',1)->where('modular_id',$item['id'])->order('top desc,create_time desc')->select()->toArray();
            foreach ($auth_info as $k=>$v){
                if($item['type']==1){
                    $auth_info[$k]['create_time'] = date('Y/m/d',$v['create_time']);
                }
                if($item['type']==4){
                    $auth_info[$k]['covers'] = [
                        [
                            'width'=> 28,
                            'height'=> 28,
                            'latitude'=> $v['latitude'],
                            'longitude'=> $v['longitude'],
                            'iconPath'=> 'https://lbqnyv2.migugu.com/images/12/2018/11/A33zQycihMM33y337LH23myTqTl3tl.png',
                            'callout'=> [
                                'content'=>$v['address'],
                                'fontSize'=>14,
                                'bgColor'=>'#ffffff',
                                'padding'=>4,
                                'display'=>'ALWAYS',
                                'textAlign'=>'center',
                                'borderRadius'=>2
                            ]
                        ]
                    ];
                }
                if($item['type'] ==7){
                    if(strstr($v['video'], '/v.qq.com/')){
                        $auth_info[$k]['vid'] = getStr($v['video']);
                    }else{
                        $auth_info[$k]['video'] = transImagesOne($v, ['video'])['video'];
                    }
                }
                if(!empty($v['content']) && strstr($v['content'], '/v.qq.com/')&&$item['type'] !=2){
                    $auth_info[$k]['content'] =  getimgs($v['content']);
                }
                if(!empty($v['introduction']) && strstr($v['introduction'], '/v.qq.com/')){
                    $auth_info[$k]['introduction'] =  getimgs($v['introduction']);
                }
                if($item['type'] ==2){
                    unset($auth_info[$k]['content']);
//                    $auth_info[$k]['introduction'] = str_replace("&amp;nbsp;","",getimgs($v['introduction']));
                    $auth_info[$k]['introduction'] = str_replace("&amp;nbsp;","",getimgsV2($v['introduction']));
                }
                if(in_array($item['type'],[1,3])&&!empty($auth_info[$k]['content'])){

                    unset($auth_info[$k]['content']);

                }
            }
            $item['list_info'] = transImagesOne($auth_info, ['cover']);
        }

        return $item;

    }




	public function lists($page,$list_rows,$where = []){
        $data = $this->where($where)->order('top desc,create_time asc')->paginate( [ 'list_rows' => $list_rows, 'page' => $page ])->toArray();
         $data = transImagesOne($data, ['cover']);
        return $data;
    }
    public function list_nopage($where = []){
        $data = $this->where($where)->order('top desc,create_time asc')->select()->toArray();
        if($data){
            foreach ($data as $key=>$val){
                 if($val['type'] == 6 || $val['type'] == 9||$val['type'] == 10){
                     $data[$key]['content'] = Db::name($val['table_name'])->where('modular_id',$val['id'])->where('status',1)->order('top','desc')->select();
                 }
            }
        }
        $data = transImagesOne($data, ['cover']);
        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-06-12 13:23
     * @功能说明:获取导航栏diy版
     */
    public function listNopageDiy($dis,$file = '*'){

        $data = $this->where($dis)->field($file)->order('top desc,id asc')->select()->toArray();
        //处理图片
        $data = transImagesOne($data, ['cover']);

        if(!empty($data)){

            foreach ($data as &$v){

                $v['link'][] = [
                    //标题
                    'title' => $v['title'],
                    //链接
                    'url'   => '/pages/website/list?id=ID&name='.$v['title'].'&mtype='.$v['type']
                ];

                $img = $v['img'];

                $v['img'] = [];

                $v['img'][0]['url'] = $img;
                //获取linktype
                switch ($v['type']){
                    //拨打电话
                    case 6:
                        $v['linkType'] = 1;
                        break;
                    //跳转小程序
                    case 10:
                        $v['linkType'] = 2;
                        break;
                    //跳转网页
                    case 9:
                        $v['linkType'] = 3;
                        break;
                    //内部页面
                    default:
                        $v['linkType'] = 4;
                        break;
                }

            }

        }
        return $data;
    }
	//获取更多
	public function listinfo($id,$page,$list_rows,$where=[],$type=0,$keyword='',$other = []){
	    $table = $this->getTable($id);
        $data = [];
	    if($table &&$table['type'] != 8){
	        if($keyword){
                if($table['type'] ==5){
                    $where[] = [ 'name', 'like', "%{$keyword}%" ];
                }else{
                    $where[] = [ 'title', 'like', "%{$keyword}%" ];
                }
            }
	        #视频模块分类检索
	        if($table['table_name'] == 'longbing_card_video' && isset($other['category_id'])){
	            $category_id = $other['category_id'];
	            if($category_id > 0){
                    $where[ 'category_id'] = $other['category_id'];
                }
            }

            $data = Db::name($table['table_name'])
                ->where($where)
                ->order('top desc,create_time desc')
                ->where('modular_id',$id)
                ->paginate([ 'list_rows' => $list_rows, 'page' => $page])->each(function($item, $key)use ($table,$type){
                    $item['type'] = $table['type'];
                    if(!empty($item['content'])){
                        if(strstr($item['content'], '/v.qq.com/')&&$type==0){
                            $item['content'] =  getimgs($item['content']);
                        }
                        if( $item['type']==1|| $item['type']==3){
                            unset($item['content']);
                        }else{
                            $item['content'] = htmlspecialchars_decode($item['content']);
                            $item['content1'] = getimgsV2($item['content']);
                        }
                    }
                    if(!empty($item['introduction'])){
                        if(strstr($item['introduction'], '/v.qq.com/')&&$type==0){
                            $item['introduction'] =  getimgs($item['introduction']);
                        }
                        $item['introduction'] = htmlspecialchars_decode($item['introduction']);
                        $item['introduction1'] = getimgsV2($item['introduction']);
                    }
                    if($type == 0){
                        $item['create_time'] = date('Y/m/d',$item['create_time']);
                    }
                    if($item['type']==4){
                        $item['covers'] = [
                            [
                                'width'=> 28,
                                'height'=> 28,
                                'latitude'=> $item['latitude'],
                                'longitude'=> $item['longitude'],
                                'iconPath'=> 'https://lbqnyv2.migugu.com/images/12/2018/11/A33zQycihMM33y337LH23myTqTl3tl.png',
                                'callout'=> [
                                    'content'=>$item['address'],
                                    'fontSize'=>14,
                                    'bgColor'=>'#ffffff',
                                    'padding'=>4,
                                    'display'=>'ALWAYS',
                                    'textAlign'=>'center',
                                    'borderRadius'=>2
                                ]
                            ]
                        ];
                    }
                    if($item['type'] ==7){
                        if(strstr($item['video'], '/v.qq.com/')){
                            $item['vid'] = getStr($item['video']);
                        }else{
                            $item['video'] = transImagesOne($item, ['video'])['video'];
                        }
                    }
                    if($type ==1){
                        unset($item['covers']);
                        unset($item['type']);
                        unset($item['vid']);
                    }

                    /**
                     * @date 2020/5/14 --lichuanming
                     */
                    if($table['table_name'] == 'longbing_card_video'){
                        $item['category'] = CardVideoCategory::where('id','=',$item['category_id'])->value('name');
                    }

                    return $item;
                })->toArray();
            $data = transImagesOne($data, ['cover']);
        }else{
	        if($type ==1){
                if($keyword){
                    $where[] = [ 'name', 'like', "%{$keyword}%" ];
                }
                $data = Db::name($table['table_name'])
                    ->where($where)
                    ->order('top desc,create_time desc')
                    ->where('modular_id',$id)
                    ->paginate([ 'list_rows' => $list_rows, 'page' => $page]);
            }else{
                $data['data'] = $this->where('id',$id)->select()->toArray();
            }
        }
        return $data;
    }
    public function getTable($id = null){
        $data = $this->where('id',$id)->find();
        if($data){
            $data = $data->toArray();
            $data = transImagesOne($data, ['cover']);
        }
        return $data;
    }
    //查询官网表中的表对应的表详情
    public function infoDetail($params,$type=0){
        $table = $this->getTable($params['modular_id']);
        $data = Db::name($table['table_name'])->where('id',$params['id'])->find();
        if(!empty($data['content'])){
            if(strstr($data['content'], '/v.qq.com/')&&$type==0){
                $data['content'] =  getimgs($data['content']);
            }
            $data['content'] = htmlspecialchars_decode($data['content']);
            $data['content1'] = getimgsV2($data['content']);
        }
        if(!empty($data['introduction'])&&$type==0){
            if(strstr($data['introduction'], '/v.qq.com/')){
                $data['introduction'] =  getimgs($data['introduction']);
            }
            $data['introduction'] = htmlspecialchars_decode($data['introduction']);
            $data['introduction1'] = getimgsV2($data['introduction']);
        }
        if($table['type'] ==4){
            $data['covers'] = [
                [
                    'width'=> 28,
                    'height'=> 28,
                    'latitude'=> $data['latitude'],
                    'longitude'=> $data['longitude'],
                    'iconPath'=> 'https://lbqnyv2.migugu.com/images/12/2018/11/A33zQycihMM33y337LH23myTqTl3tl.png',
                    'callout'=> [
                        'content'=>$data['address'],
                        'fontSize'=>14,
                        'bgColor'=>'#ffffff',
                        'padding'=>4,
                        'display'=>'ALWAYS',
                        'textAlign'=>'center',
                        'borderRadius'=>2
                    ]
                ]
            ];
        }
        if($table['type'] ==7){
            if(strstr($data['video'], '/v.qq.com/')){
                $data['vid'] = getStr($data['video']);
            }else{
                $data['video'] = transImagesOne($data, ['video'])['video'];
            }
        }
        if($type==1){
            unset($data['covers']);
            unset($data['type']);
            unset($data['vid']);
        }
        $data = datachange($data);
        return transImagesOne($data, ['cover']);
    }
    public function updateDetail($params,$data){
        $table = $this->getTable($params['modular_id']);
        if(!empty($data['cover'])){
            $data['cover'] = trim( implode( ',', $data[ 'cover' ]), ',' );
        }
        if(!empty($data['content'])){
            $data['content'] = htmlspecialchars($data['content']);
        }
        if(!empty($data['introduction'])){
            $data['introduction'] = htmlspecialchars($data['introduction']);
        }
        $data['update_time'] = time();
        unset($data['modular_id']);

        //文章定时发布
        if($table['table_name']=='longbing_card_articles'&&!empty($data['is_release'])){

            if($data['release_time']>time()){

                $data['status'] = 0;
            }

        }

        return Db::name($table['table_name'])->where('id',$params['id'])->update($data);
    }
    public function createDetail($params){
	    if(!empty($params['cover'])){
            $params['cover'] = $params['cover']?$params['cover'][0]:[];
        }
        if(!empty($params['content'])){
            $params['content'] = htmlspecialchars($params['content']);
        }
        if(!empty($params['introduction'])){
            $params['introduction'] = htmlspecialchars($params['introduction']);
        }
        $table = $this->getTable($params['modular_id']);
        $params['create_time'] = time();
        //文章定时发布
        if($table['table_name']=='longbing_card_articles'&&!empty($params['is_release'])){

            if($params['release_time']>time()){

                $params['status'] = 0;
            }

        }
        return Db::name($table['table_name'])->insert($params);
    }
    public function change_status($data){
	    $status = $this->where('id',$data['id'])->value('status');
        $sta = $status==1?0:1;
        $where['id'] = $data['id'];
        $data['status'] = $sta;
        return $this->upsave($where,$data);
    }
    //类型关联表
    public function type_table($type){
	    $arr = [
	        1=>'longbing_card_articles',
            2=>'longbing_card_desc',
            3=>'longbing_card_jobs',
            4=>'longbing_card_contact',
            5=>'longbing_card_staffs',
            6=>'longbing_card_call',
            7=>'longbing_card_video',
            8=>'longbing_card_form',
            9=>'longbing_card_web',
            10=>'longbing_card_mini'
        ];
	    return $arr[$type];
    }
    public function getID($id = null){
	    return $this->where('id',$id)->value('id');
    }


    /**
     * @author chenniang
     * @DataTime: 2021-08-13 14:55
     * @功能说明:定时发布文章
     */
    public function releaseTimeArticle(){

        $article_model = new CardArticles();

        $dis[] = ['release_time','<',time()];

        $dis[] = ['is_release','=',1];

        $dis[] = ['have_release','=',0];

        $dis[] = ['status','=',0];

        $res = $article_model->where($dis)->update(['status'=>1,'have_release'=>1]);

        return $res;
    }
}
